package Service;

import Dao.JournalismMapper;
import Dao.UserMapper;
import Mybatis_until.MybaitsUntil;
import Pojo.NewInfo;
import Pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import java.util.List;

public class ViceFindInfo {

//    寻找用户信息
    public List<User> finduserinfo() {
        SqlSession sqlSession = MybaitsUntil.getSqlSession();
        UserMapper  userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.finduserinfo();
        sqlSession.close();
        return users;
    }

//    发布文章
    public void addnew(String title, String simply, String content, String type) {
        SqlSession sqlSession = MybaitsUntil.getSqlSession();
        JournalismMapper  j = sqlSession.getMapper(JournalismMapper.class);
        Pojo.NewInfo newInfo = new Pojo.NewInfo();
        newInfo.setTitle(title);
        newInfo.setSimply(simply);
        newInfo.setContent(content);
        newInfo.setType(type);
        j.addnew(newInfo);
        sqlSession.close();
    }

    public NewInfo findidinfo(String id) {
        SqlSession sqlSession = MybaitsUntil.getSqlSession();
        JournalismMapper j = sqlSession.getMapper(JournalismMapper.class);
        NewInfo newInfo = j.findidinfo(id);
        System.out.println(newInfo);
        sqlSession.close();
        return newInfo;
    }

    static Logger logger = Logger.getLogger(JournalismMapper.class);
//    修改新闻
    public int updatanew(String idd, String title, String simply, String content, String type, String time) {
        SqlSession sqlSession = MybaitsUntil.getSqlSession();
        logger.info("开始");
        JournalismMapper j = sqlSession.getMapper(JournalismMapper.class);
        int iddd = Integer.parseInt(idd);
        System.out.println(idd);
        NewInfo newInfo = new NewInfo(iddd,content,title,simply,type,time);
        int x = j.updatanew(newInfo);
        sqlSession.close();
        return x;
    }

//    根据id删除新闻
    public int delectnew(String id) {
        SqlSession sqlSession = MybaitsUntil.getSqlSession();
        JournalismMapper j = sqlSession.getMapper(JournalismMapper.class);
        int x = j.delectnew(id);
        sqlSession.close();
        return x;
    }
}
